package com.aaa.stock.service.medic.impl;

import com.aaa.entity.Medicines;
import com.aaa.entity.Producer;
import com.aaa.entity.Role;
import com.aaa.stock.dao.medic.MedicinesDao;
import com.aaa.stock.dao.prod.ProducerDao;
import com.aaa.stock.service.medic.StoMediService;
import com.aaa.stock.service.prod.ProdcerService;
import com.aaa.stock.vo.MedicinesVo;
import com.aaa.stock.vo.ProducerVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MedicinesServiceImpl extends ServiceImpl<MedicinesDao,Medicines> implements StoMediService {
    @Autowired
    private MedicinesDao medicinesDao;

    @Override
    public Result<IPage<Medicines>> findByListtion(Integer currentPage, Integer pageSize, MedicinesVo medicinesVo) {
        IPage<Medicines> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Medicines> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(medicinesVo.getProducerId())){
            wrapper.eq("producter_id",medicinesVo.getProducerId());
        }
        if(StringUtils.hasText(medicinesVo.getMedicinesName())){
            wrapper.like("medicines_name",medicinesVo.getMedicinesName());
        }
        if(StringUtils.hasText(medicinesVo.getMedicinesType())){
            wrapper.eq("medicines_type",medicinesVo.getMedicinesType());
        }
        if(StringUtils.hasText(medicinesVo.getKeywords())){
            wrapper.eq("keywords",medicinesVo.getKeywords());
        }
        if(Objects.nonNull(medicinesVo.getPrescriptionType())){
            wrapper.eq("prescription_type",medicinesVo.getPrescriptionType());
        }
        if(Objects.nonNull(medicinesVo.getStatus())){
            wrapper.eq("status",medicinesVo.getStatus());
        }

        IPage<Medicines> page1 = medicinesDao.selectPage(page, wrapper);
        return new Result<IPage<Medicines>>(2000,"查询厂家成功",page1);
    }

    @Override
    public Result<Map<String, Object>> deleteMedic(Long medicinesIds) {
        medicinesDao.deleteMedic(medicinesIds);
        return new Result<>(2000,"删除成功");
    }

    @Override
    public Page<Medicines> getMedicAll(MedicinesVo medicinesVo) {
        Page<Medicines> objectPage = new Page<>(medicinesVo.getCurrent(), medicinesVo.getSize());
        QueryWrapper<Medicines> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(medicinesVo.getKeywords())){
            wrapper.eq("keywords",medicinesVo.getKeywords());
        }
        Page<Medicines> page = medicinesDao.selectPage(objectPage, wrapper);
        return page;
    }

    @Override
    public Boolean medicinesInventory(String num, String itemName) {

        BigDecimal bigDecimal = new BigDecimal(num);
        QueryWrapper<Medicines> wrapper=new QueryWrapper<>();
        wrapper.eq("medicines_name",itemName);
        Medicines medicines = medicinesDao.selectOne(wrapper);
        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(medicines.getMedicinesStockNum()));
        BigDecimal subtract = bigDecimal1.subtract(bigDecimal);
        if (subtract.compareTo(BigDecimal.valueOf(0))>-1) {
            BigDecimal i=bigDecimal1.subtract(bigDecimal);
            Medicines medicines1 = new Medicines();
            Integer i1 = i.intValue();
            medicines1.setMedicinesStockNum(BigDecimal.valueOf(i1));
            QueryWrapper<Medicines> wrapper1=new QueryWrapper<>();
            wrapper1.eq("medicines_name",itemName);
            int update = medicinesDao.update(medicines1, wrapper1);
            return update>0?true:false;
        }
        return false;
    }


}
